package zhoug.zui.task;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.Guideline;
import androidx.core.content.ContextCompat;
import zhoug.common.utils.AttributeUtils;
import zhoug.common.utils.RegexUtils;
import zhoug.common.utils.ResourceUtils;
import zhoug.common.utils.SpannableUtils;
import zhoug.common.utils.StringUtils;
import zhoug.common.utils.file.IFile;
import zhoug.zui.R;
import zhoug.zui.ZUIFilePreview;
import zhoug.zui.ZUITextView;

/**
 * 任务流程图
 *
 * @Author: zhoug
 * @Date: 2024-07-17
 * @Description:
 */
public class ZUITaskTimeFlowView extends LinearLayout {
    private static final String TAG = ">>>ZUITaskTimeFlowView";
    private static boolean DEBUG = true;
    protected List<TaskTimeFlowData> mData;
    //#########线和园的属性######################
    /**
     * 任务完成状态时的圆和线的颜色
     */
    protected int completeLineColor;
    /**
     * 任务未完成状态时的圆和线的颜色
     */
    protected int unCompleteLineColor;
    /**
     * 圆的大小:宽高
     */
    protected int circleSize;
    //#########时间属性######################
    protected int timeGravity = Gravity.CENTER;
    /**
     * 时间颜色
     */
    protected int timeColor;
    /**
     * 时间的文字大小
     * 12:05:35
     * 2024-02-13
     */
    protected float timeSize;
    /**
     * 时间文字大小自适应
     */
    protected boolean autoTextSize;
    /**
     * 时间下面的日期占文字大小{@link #timeSize}的比率
     * eg:2024-02-13
     */
    protected float timeSizeProportion = 0.7f;
    /**
     * 时间02:12:25 Typeface{@link Typeface#NORMAL}...
     */
    protected int timeTypeface;
    /**
     * 日期 2023-12-01 Typeface{@link Typeface#NORMAL}...
     */
    protected int dateTypeface;

    /**
     * 时间和圆的距离
     */
    protected int timeMarginLine;
    //#########节点名称的属性######################

    /**
     * 任务完成状态时的节点名称颜色
     */
    protected int completeNodeNameColor;
    /**
     * 任务未完成状态时的节点名称颜色
     */
    protected int unCompleteNodeNameColor;
    /**
     * 节点名称大小
     */
    protected float nodeNameSize;

    /**
     * 节点名称Typeface{@link Typeface#NORMAL}...
     */
    protected int nodeNameTypeface;
    /**
     * 节点名称和线的距离
     */
    protected int nodeNameMarginLine;
    /**
     * 节点名称和下面的内容距离
     */
    protected int nodeNameMarginContent;
    /**
     * 节点名词扩增组件距离节点名称的距离
     */
    protected int extrasMarginStart;
    /**
     * 扩展组件文字颜色
     */
    protected int extrasTextColor;
    /**
     * 扩展组件文字大小
     */
    protected float extrasTextSize;
    /**
     * 扩展组件背景
     */
    protected @DrawableRes int extrasBackground;

    /**
     * 扩展组件左边图标颜色
     */
    protected @ColorRes int extrasDrawableTint;
    /**
     * 扩展组件图标和文字间距
     */
    protected int extrasDrawablePadding;
    /**
     * 扩展组件图标大小
     */
    protected int extrasDrawableSize;

    //#########节点内容LineData属性######################

    /**
     * LineData 文字大小
     */
    protected float lineDataSize;
    /**
     * LineData 名称颜色
     */
    protected int lineDataNameColor;
    /**
     * LineData value颜色
     */
    protected int lineDataValueColor;
    /**
     * LineData 名称和值得间距
     */
    protected int lineDataNameMarginValue;

    /**
     * LineData的name对齐文字数量
     */
    protected int lineDataNameAlignLength = 0;
    /**
     * LineData的name后面是否加上冒号
     */
    protected boolean lineDataNameColon = true;
    /**
     * LineData之间的行间距
     */
    protected int lineDataLineMargin;
    /**
     * 文件每行数量
     */
    protected int fileSpanCount;
    /**
     * 左右比
     * 默认0.28
     */
    protected float leftRightRatio = 0.28f;

    protected LayoutInflater mLayoutInflater;
    /**
     * 点击节点名称监听
     */
    private OnNodeClickListener onNodeClickListener;
    /**
     * 点击扩展按钮监听
     */
    private  OnExtraButtonClickListener onExtraButtonClickListener;
    /**
     * 点击LineData容器监听
     */
    private  OnLineDataClickListener onLineDataClickListener;
    /**
     * 点击时间监听
     */
    private  OnTimeClickListener onTimeClickListener;
    /**
     * 自定义文件预览接口
     */
    private ZUIFilePreview.OnPreviewFileClickListener onPreviewFileClickListener;

    public ZUITaskTimeFlowView(Context context) {
        this(context, null);
    }

    public ZUITaskTimeFlowView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, R.attr.ZUITaskTimeFlowView_theme);
    }

    public ZUITaskTimeFlowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);

    }


    @SuppressLint("ResourceType")
    protected void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        Log.d(TAG, "R.styleable.ZUITaskTimeFlowView_zui_completeLineColor:" + R.styleable.ZUITaskTimeFlowView_zui_completeLineColor);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ZUITaskTimeFlowView, defStyleAttr, R.style.ZUITaskTimeFlowView_def);
        completeLineColor = a.getColor(R.styleable.ZUITaskTimeFlowView_zui_completeLineColor, ContextCompat.getColor(context, R.color.zui_colorPrimaryDark));
        unCompleteLineColor = a.getColor(R.styleable.ZUITaskTimeFlowView_zui_unCompleteLineColor, ContextCompat.getColor(context, R.color.zui_gray));
        circleSize = a.getDimensionPixelOffset(R.styleable.ZUITaskTimeFlowView_zui_circleSize, ResourceUtils.dpTopx(14));
        timeColor = a.getColor(R.styleable.ZUITaskTimeFlowView_zui_timeColor, ContextCompat.getColor(context, R.color.zui_textColor));
        timeSize = a.getDimension(R.styleable.ZUITaskTimeFlowView_zui_timeSize, ResourceUtils.spTopx(14));
        timeSizeProportion = a.getFloat(R.styleable.ZUITaskTimeFlowView_zui_timeSizeProportion, 0.7f);
        timeTypeface = a.getInt(R.styleable.ZUITaskTimeFlowView_zui_timeTypeface, Typeface.NORMAL);
        dateTypeface = a.getInt(R.styleable.ZUITaskTimeFlowView_zui_dateTypeface, Typeface.NORMAL);
        timeMarginLine = a.getDimensionPixelOffset(R.styleable.ZUITaskTimeFlowView_zui_timeMarginLine, ResourceUtils.dpTopx(25));
        completeNodeNameColor = a.getColor(R.styleable.ZUITaskTimeFlowView_zui_completeNodeNameColor, ContextCompat.getColor(context, R.color.zui_colorPrimaryDark));
        unCompleteNodeNameColor = a.getColor(R.styleable.ZUITaskTimeFlowView_zui_unCompleteNodeNameColor, ContextCompat.getColor(context, R.color.zui_gray));
        nodeNameSize = a.getDimension(R.styleable.ZUITaskTimeFlowView_zui_nodeNameSize, ResourceUtils.spTopx(16));
        nodeNameTypeface = a.getInt(R.styleable.ZUITaskTimeFlowView_zui_nodeNameTypeface, Typeface.BOLD);
        nodeNameMarginLine = a.getDimensionPixelOffset(R.styleable.ZUITaskTimeFlowView_zui_nodeNameMarginLine, ResourceUtils.dpTopx(25));
        nodeNameMarginContent = a.getDimensionPixelOffset(R.styleable.ZUITaskTimeFlowView_zui_nodeNameMarginContent, ResourceUtils.dpTopx(10));
        extrasMarginStart = a.getDimensionPixelOffset(R.styleable.ZUITaskTimeFlowView_zui_extrasMarginStart, ResourceUtils.dpTopx(10));
        extrasTextColor = a.getColor(R.styleable.ZUITaskTimeFlowView_zui_extrasTextColor, ContextCompat.getColor(context, R.color.zui_colorPrimaryDark));
        extrasTextSize = a.getDimension(R.styleable.ZUITaskTimeFlowView_zui_extrasTextSize, ResourceUtils.spTopx(14));
        extrasBackground = a.getResourceId(R.styleable.ZUITaskTimeFlowView_zui_extrasBackground, R.drawable.zui_border_theme_bg);
        extrasDrawableTint = a.getResourceId(R.styleable.ZUITaskTimeFlowView_zui_extrasDrawableTint, 0);
        extrasDrawablePadding = a.getDimensionPixelSize(R.styleable.ZUITaskTimeFlowView_zui_extrasDrawablePadding, 0);
        extrasDrawableSize = a.getDimensionPixelSize(R.styleable.ZUITaskTimeFlowView_zui_extrasDrawableSize, ResourceUtils.dpTopx(12));


        lineDataSize = a.getDimension(R.styleable.ZUITaskTimeFlowView_zui_lineDataSize, ResourceUtils.spTopx(14));
        lineDataNameColor = a.getColor(R.styleable.ZUITaskTimeFlowView_zui_lineDataNameColor, ContextCompat.getColor(context, R.color.zui_textColor));
        lineDataValueColor = a.getColor(R.styleable.ZUITaskTimeFlowView_zui_lineDataValueColor, ContextCompat.getColor(context, R.color.zui_textColor));
        lineDataNameMarginValue = a.getDimensionPixelOffset(R.styleable.ZUITaskTimeFlowView_zui_lineDataNameMarginValue, ResourceUtils.dpTopx(10));
        lineDataNameAlignLength = a.getInt(R.styleable.ZUITaskTimeFlowView_zui_lineDataNameAlignLength, 0);
        lineDataNameColon = a.getBoolean(R.styleable.ZUITaskTimeFlowView_zui_lineDataNameColon, true);
        lineDataLineMargin = a.getDimensionPixelOffset(R.styleable.ZUITaskTimeFlowView_zui_lineDataLineMargin, ResourceUtils.dpTopx(10));
        fileSpanCount = a.getInt(R.styleable.ZUITaskTimeFlowView_zui_fileSpanCount, 3);
        leftRightRatio = a.getFloat(R.styleable.ZUITaskTimeFlowView_zui_leftRightRatio, 0.28f);
        timeGravity = a.getInt(R.styleable.ZUITaskTimeFlowView_zui_timeGravity, Gravity.CENTER);
        autoTextSize = a.getBoolean(R.styleable.ZUITaskTimeFlowView_zui_autoTextSize, false);

        //        Object xxx=a.xxx( R.styleable.ZUITaskTimeFlowView_);
        a.recycle();
        setOrientation(VERTICAL);
        mLayoutInflater = LayoutInflater.from(getContext());

    }

    /**
     * 根据数据{@link #mData}加载流程图
     */
    protected void layoutTraceFlow() {
        removeAllViews();
        if (mData != null && !mData.isEmpty()) {
            int dataSize = mData.size();
            for (int i = 0; i < dataSize; i++) {
                TaskTimeFlowData taskFlowImageData = mData.get(i);
                FlowHolder flowHolder = createFlowHolder();
                flowHolder.setTime(taskFlowImageData.getTime());
                flowHolder.tvName.setText(taskFlowImageData.getName());
                boolean extrasEnable = taskFlowImageData.isExtrasEnable();
                if (extrasEnable) {
                    flowHolder.tvExtras.setText(taskFlowImageData.getExtrasName());
                    int extrasIcon = taskFlowImageData.getExtrasIcon();
                    if (extrasIcon != 0) {
                        Drawable icon = AttributeUtils.getDrawable(getContext(), extrasIcon, extrasDrawableTint);
                        if (icon != null) {
                            flowHolder.tvExtras.setCompoundDrawables(icon, null, null, null);
                        }
                    }
                    flowHolder.tvExtras.setVisibility(VISIBLE);
                } else {
                    flowHolder.tvExtras.setVisibility(GONE);
                }
                //点击名称监听
                if(onNodeClickListener!=null){
                    flowHolder.tvName.setClickable(true);
                    flowHolder.tvName.setTag(taskFlowImageData);
                    flowHolder.tvName.setOnClickListener(onClickListener);
                }else{
                    flowHolder.tvName.setClickable(false);
                }
                //点击extras按钮监听
                if(onExtraButtonClickListener!=null){
                    flowHolder.tvExtras.setClickable(true);
                    flowHolder.tvExtras.setTag(taskFlowImageData);
                    flowHolder.tvExtras.setOnClickListener(onClickListener);
                }else{
                    flowHolder.tvExtras.setClickable(false);
                }
                //点击时间监听
                if(onTimeClickListener!=null){
                    flowHolder.tvTime.setClickable(true);
                    flowHolder.tvTime.setTag(taskFlowImageData);
                    flowHolder.tvTime.setOnClickListener(onClickListener);
                }else{
                    flowHolder.tvTime.setClickable(false);
                }


                //点击LineData按钮监听
                if(onLineDataClickListener!=null){
                    flowHolder.lineContainer.setClickable(true);
                    flowHolder.lineContainer.setTag(taskFlowImageData);
                    flowHolder.lineContainer.setOnClickListener(onClickListener);
                }else{
                    flowHolder.lineContainer.setClickable(false);
                }

                List<TaskTimeFlowData.LineData> lines = taskFlowImageData.getLines();
                if (lines != null && !lines.isEmpty()) {
                    int lastLineIndex = lines.size() - 1;
                    for (int lineIndex = 0; lineIndex <= lastLineIndex; lineIndex++) {
                        TaskTimeFlowData.LineData item = lines.get(lineIndex);
                        View lineView = null;
                        if (item instanceof TaskTimeFlowData.TextLineData) {
                            lineView = addTextLineData(flowHolder.lineContainer, (TaskTimeFlowData.TextLineData) item);
                        } else if (item instanceof TaskTimeFlowData.FileLineData) {
                            lineView = addFileLineData(flowHolder.lineContainer, (TaskTimeFlowData.FileLineData) item);

                        }
                        if (lineView != null && lineIndex < lastLineIndex) {
                            ViewGroup.LayoutParams layoutParams = lineView.getLayoutParams();
                            if (layoutParams instanceof MarginLayoutParams marginLayoutParams) {
                                marginLayoutParams.bottomMargin = lineDataLineMargin;
                            }
                        }
                    }
                }
                if (taskFlowImageData.isComplete()) {
                    flowHolder.tvName.setTextColor(completeNodeNameColor);
                    AttributeUtils.setBackgroundTintList(flowHolder.tvCircle, ColorStateList.valueOf(completeLineColor));
                    setLineStyle(flowHolder.topLine, true);
                } else {
                    flowHolder.tvName.setTextColor(unCompleteNodeNameColor);
                    AttributeUtils.setBackgroundTintList(flowHolder.tvCircle, ColorStateList.valueOf(unCompleteLineColor));
                    setLineStyle(flowHolder.topLine, false);
                }
                if (i + 1 < dataSize) {
                    TaskTimeFlowData nextData = mData.get(i + 1);
                    setLineStyle(flowHolder.bottomLine, nextData.isComplete());
                }


                if (i == 0) {
                    flowHolder.topLine.setVisibility(View.GONE);
                }
                if (i == dataSize - 1) {
                    flowHolder.bottomLine.setVisibility(View.GONE);
                }
                /*if (i > 0 && i < dataSize - 1) {
                    ViewGroup.LayoutParams circleLayoutParams = flowHolder.tvCircle.getLayoutParams();
                    circleLayoutParams.width = circle2Size;
                    circleLayoutParams.height = circle2Size;
                    flowHolder.tvCircle.setLayoutParams(circleLayoutParams);
                }*/
                addView(flowHolder.rootView);
            }


        }
    }


    /**
     * 创建一个流程
     *
     * @return
     */
    private FlowHolder createFlowHolder() {
        View flowRoot = mLayoutInflater.inflate(R.layout.zui_task_time_flow_view_item, this, false);
        FlowHolder flowHolder = new FlowHolder();
        flowHolder.rootView = flowRoot;
        flowHolder.guideLine = flowRoot.findViewById(R.id.guideLine);
        flowHolder.containerNode = flowRoot.findViewById(R.id.container_node);
        flowHolder.topLine = flowRoot.findViewById(R.id.topLine);
        flowHolder.bottomLine = flowRoot.findViewById(R.id.bottomLine);
        flowHolder.tvTime = flowRoot.findViewById(R.id.tv_Time);
        flowHolder.tvCircle = flowRoot.findViewById(R.id.tv_circle);
        flowHolder.tvName = flowRoot.findViewById(R.id.tv_name);
        flowHolder.tvExtras = flowRoot.findViewById(R.id.tv_nameExtras);
        flowHolder.lineContainer = flowRoot.findViewById(R.id.container_lines);

        if (circleSize > 0) {
            ViewGroup.LayoutParams layoutParams = flowHolder.tvCircle.getLayoutParams();
            if (layoutParams != null) {
                layoutParams.width = circleSize;
                layoutParams.height = circleSize;
                if (layoutParams instanceof MarginLayoutParams marginLayoutParams) {
                    marginLayoutParams.setMarginEnd(timeMarginLine);
                }
            }

        }
        if (timeColor != 0) {
            AttributeUtils.setTextColor(flowHolder.tvTime, timeColor);
        }
        if (timeSize != 0) {
            AttributeUtils.setTextSize(flowHolder.tvTime, timeSize);
            if (autoTextSize) {
                AttributeUtils.setAutoTextSize(flowHolder.tvTime, ResourceUtils.spTopx(6), (int) timeSize, ResourceUtils.spTopx(1), TypedValue.COMPLEX_UNIT_PX);
            }
        }
        flowHolder.tvTime.setGravity(timeGravity);
        AttributeUtils.setTypeface(flowHolder.tvTime, timeTypeface);
        if (nodeNameSize != 0) {
            AttributeUtils.setTextSize(flowHolder.tvName, nodeNameSize);
        }
        AttributeUtils.setTypeface(flowHolder.tvName, nodeNameTypeface);
        ViewGroup.LayoutParams layoutParams = flowHolder.containerNode.getLayoutParams();
        if (layoutParams instanceof MarginLayoutParams marginLayoutParams) {
            marginLayoutParams.setMarginStart(nodeNameMarginLine);
        }

        layoutParams = flowHolder.lineContainer.getLayoutParams();
        if (layoutParams instanceof MarginLayoutParams marginLayoutParams) {
            marginLayoutParams.setMarginStart(nodeNameMarginContent);
        }

        layoutParams = flowHolder.guideLine.getLayoutParams();
        if (layoutParams instanceof ConstraintLayout.LayoutParams consLayoutParams) {
            if (consLayoutParams.guidePercent != leftRightRatio) {
                consLayoutParams.guidePercent = leftRightRatio;
            }
        }
        //flowHolder.tvExtras

        AttributeUtils.setTextColor(flowHolder.tvExtras, extrasTextColor, 0);
        AttributeUtils.setTextSize(flowHolder.tvExtras, extrasTextSize, 0);
        flowHolder.tvExtras.setBackgroundResource(extrasBackground);
        if (extrasDrawableSize != 0) {
            flowHolder.tvExtras.setDrawableSize(extrasDrawableSize, extrasDrawableSize);
        }

        flowHolder.tvExtras.setCompoundDrawablePadding(extrasDrawablePadding);
        layoutParams = flowHolder.tvExtras.getLayoutParams();
        if (layoutParams instanceof MarginLayoutParams marginLayoutParams) {
            marginLayoutParams.setMarginStart(extrasMarginStart);
        }
        return flowHolder;
    }

    /**
     * 设置任务连接线的样式
     *
     * @param lineView
     * @param complete
     */
    private void setLineStyle(View lineView, boolean complete) {
        if (complete) {
            Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.zui_task_time_flow_line);
            if (drawable != null) {
                lineView.setBackground(drawable);
                AttributeUtils.setBackgroundTintList(lineView, ColorStateList.valueOf(completeLineColor));
            }
        } else {
            Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.zui_task_time_flow_dashline);
            if (drawable != null) {
                lineView.setBackground(drawable);
                AttributeUtils.setBackgroundTintList(lineView, ColorStateList.valueOf(unCompleteLineColor));

            }
        }
    }

    /**
     * 给每个任务添加文字
     *
     * @param lineGroup
     * @param lineData
     */
    private View addTextLineData(LinearLayout lineGroup, TaskTimeFlowData.TextLineData lineData) {
        View lineView = mLayoutInflater.inflate(R.layout.zui_task_time_flow_view_text, lineGroup, false);
        TextView tvName = lineView.findViewById(R.id.tv_flow_line_name);
        TextView tvValue = lineView.findViewById(R.id.tv_flow_line_value);
        setLineName(tvName, lineData.getName());
        tvValue.setText(lineData.getText());
        if (lineDataSize != 0) {
            AttributeUtils.setTextSize(tvName, lineDataSize);
            AttributeUtils.setTextSize(tvValue, lineDataSize);
        }
        if (lineDataNameColor != 0) {
            AttributeUtils.setTextColor(tvName, lineDataNameColor);
        }
        if (lineDataValueColor != 0) {
            AttributeUtils.setTextColor(tvValue, lineDataValueColor);
        }

        ViewGroup.LayoutParams layoutParams = tvName.getLayoutParams();
        if (layoutParams instanceof MarginLayoutParams marginLayoutParams) {
            marginLayoutParams.setMarginEnd(lineDataNameMarginValue);
        }
        lineGroup.addView(lineView);
        return lineView;
    }

    /**
     * 给每个任务添加图片
     *
     * @param lineGroup
     * @param lineData
     */
    private View addFileLineData(LinearLayout lineGroup, TaskTimeFlowData.FileLineData lineData) {
        List<IFile> files = lineData.getFiles();
        if (files != null && !files.isEmpty()) {
            View lineView = mLayoutInflater.inflate(R.layout.zui_task_time_flow_view_image, lineGroup, false);
            TextView tvName = lineView.findViewById(R.id.tv_flow_line_name);
            ZUIFilePreview ivValue = lineView.findViewById(R.id.flow_line_file);
            setLineName(tvName, lineData.getName());
            ivValue.setSpanCount(3);
            ivValue.setFiles(files);
            if(onPreviewFileClickListener!=null){
                ivValue.setOnPreviewFileClickListener(onPreviewFileClickListener);
            }

            if (lineDataSize != 0) {
                AttributeUtils.setTextSize(tvName, lineDataSize);
            }
            if (lineDataNameColor != 0) {
                AttributeUtils.setTextColor(tvName, lineDataNameColor);
            }
            ViewGroup.LayoutParams layoutParams = tvName.getLayoutParams();
            if (layoutParams instanceof MarginLayoutParams marginLayoutParams) {
                marginLayoutParams.setMarginEnd(lineDataNameMarginValue);
            }
            ivValue.setSpanCount(fileSpanCount);
            lineGroup.addView(lineView);
            return lineView;
        }
        return null;
    }

    /**
     * 设置任务每一行的名称
     *
     * @param tvName
     * @param name
     */
    private void setLineName(TextView tvName, String name) {
        if (lineDataNameColon) {
            tvName.setText(StringUtils.format("%s:", StringUtils.getAlignName(name, lineDataNameAlignLength)));
        } else {
            tvName.setText(name);
        }
    }

    private final OnClickListener onClickListener=new OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            Object tag = v.getTag();
            if(tag instanceof TaskTimeFlowData data){
                if(id==R.id.tv_name){
                    if(onNodeClickListener!=null){
                        onNodeClickListener.onNodeClick(ZUITaskTimeFlowView.this,data);
                    }
                }else if(id==R.id.tv_nameExtras){
                    if(onExtraButtonClickListener!=null){
                        onExtraButtonClickListener.onExtraButtonClick(ZUITaskTimeFlowView.this,data);
                    }
                }else if(id==R.id.container_lines){
                    if(onLineDataClickListener!=null){
                        onLineDataClickListener.onLineDataClick(ZUITaskTimeFlowView.this,data);
                    }
                }else if(id==R.id.tv_Time){
                    if(onTimeClickListener!=null){
                        onTimeClickListener.onTimeClick(ZUITaskTimeFlowView.this,data);
                    }
                }
            }

        }
    };





    public void setData(List<TaskTimeFlowData> data) {
        if (data != mData) {
            this.mData = data;
            layoutTraceFlow();
        }
    }

    public void setCompleteLineColor(int completeLineColor) {
        this.completeLineColor = completeLineColor;
    }

    public void setUnCompleteLineColor(int unCompleteLineColor) {
        this.unCompleteLineColor = unCompleteLineColor;
    }

    public void setCircleSize(int circleSize) {
        this.circleSize = circleSize;
    }

    public void setTimeColor(int timeColor) {
        this.timeColor = timeColor;
    }

    public void setTimeSize(float timeSize) {
        this.timeSize = timeSize;
    }

    public void setTimeSizeProportion(float timeSizeProportion) {
        if (timeSizeProportion > 0 && timeSizeProportion <= 1) {
            this.timeSizeProportion = timeSizeProportion;
        } else {
            this.timeSizeProportion = 0.7f;
        }
    }

    public void setTimeTypeface(int timeTypeface) {
        this.timeTypeface = timeTypeface;
    }

    public void setDateTypeface(int dateTypeface) {
        this.dateTypeface = dateTypeface;
    }

    public void setTimeMarginLine(int timeMarginLine) {
        this.timeMarginLine = timeMarginLine;
    }

    public void setCompleteNodeNameColor(int completeNodeNameColor) {
        this.completeNodeNameColor = completeNodeNameColor;
    }

    public void setUnCompleteNodeNameColor(int unCompleteNodeNameColor) {
        this.unCompleteNodeNameColor = unCompleteNodeNameColor;
    }

    public void setNodeNameSize(float nodeNameSize) {
        this.nodeNameSize = nodeNameSize;
    }

    public void setNodeNameTypeface(int nodeNameTypeface) {
        this.nodeNameTypeface = nodeNameTypeface;
    }

    public void setNodeNameMarginLine(int nodeNameMarginLine) {
        this.nodeNameMarginLine = nodeNameMarginLine;
    }

    public void setNodeNameMarginContent(int nodeNameMarginContent) {
        this.nodeNameMarginContent = nodeNameMarginContent;
    }

    public void setLineDataSize(float lineDataSize) {
        this.lineDataSize = lineDataSize;
    }

    public void setLineDataNameColor(int lineDataNameColor) {
        this.lineDataNameColor = lineDataNameColor;
    }

    public void setLineDataValueColor(int lineDataValueColor) {
        this.lineDataValueColor = lineDataValueColor;
    }

    public void setLineDataNameMarginValue(int lineDataNameMarginValue) {
        this.lineDataNameMarginValue = lineDataNameMarginValue;
    }

    public void setLineDataNameAlignLength(int lineDataNameAlignLength) {
        this.lineDataNameAlignLength = lineDataNameAlignLength;
    }

    public void setLineDataNameColon(boolean lineDataNameColon) {
        this.lineDataNameColon = lineDataNameColon;
    }

    public void setLineDataLineMargin(int lineDataLineMargin) {
        this.lineDataLineMargin = lineDataLineMargin;
    }

    public void setFileSpanCount(int fileSpanCount) {
        this.fileSpanCount = fileSpanCount;
    }

    public void setLeftRightRatio(float leftRightRatio) {
        if (leftRightRatio > 0 && leftRightRatio < 1) {
            this.leftRightRatio = leftRightRatio;
        } else {
            this.leftRightRatio = 0.28f;
        }
    }

    /**
     * @param timeGravity {@link Gravity}
     */
    public void setTimeGravity(int timeGravity) {
        this.timeGravity = timeGravity;
    }

    /**
     * 左边的时间文字大小自适应
     *
     * @param autoTextSize
     */
    public void setAutoTextSize(boolean autoTextSize) {
        this.autoTextSize = autoTextSize;
    }

    public void setExtrasMarginStart(int extrasMarginStart) {
        this.extrasMarginStart = extrasMarginStart;
    }

    public void setExtrasTextColor(int extrasTextColor) {
        this.extrasTextColor = extrasTextColor;
    }

    public void setExtrasTextSize(float extrasTextSize) {
        this.extrasTextSize = extrasTextSize;
    }

    public void setExtrasBackground(int extrasBackground) {
        this.extrasBackground = extrasBackground;
    }


    public void setExtrasDrawableTint(@ColorRes int extrasDrawableTint) {
        this.extrasDrawableTint = extrasDrawableTint;
    }

    public void setExtrasDrawablePadding(int extrasDrawablePadding) {
        this.extrasDrawablePadding = extrasDrawablePadding;
    }

    public void setExtrasDrawableSize(int extrasDrawableSize) {
        this.extrasDrawableSize = extrasDrawableSize;
    }


    public void setOnNodeClickListener(OnNodeClickListener onNodeClickListener) {
        this.onNodeClickListener = onNodeClickListener;
    }



    public void setOnExtraButtonClickListener(OnExtraButtonClickListener onExtraButtonClickListener) {
        this.onExtraButtonClickListener = onExtraButtonClickListener;
    }

    public void setOnLineDataClickListener(OnLineDataClickListener onLineDataClickListener) {
        this.onLineDataClickListener = onLineDataClickListener;
    }


    public void setOnTimeClickListener(OnTimeClickListener onTimeClickListener) {
        this.onTimeClickListener = onTimeClickListener;
    }

    public void setOnPreviewFileClickListener(ZUIFilePreview.OnPreviewFileClickListener onPreviewFileClickListener) {
        this.onPreviewFileClickListener = onPreviewFileClickListener;
    }

    /**
     * 每一个任务TaskTimeFlowData布局
     */
    private class FlowHolder {
        private View rootView;
        private View topLine;//圆上面的线
        private LinearLayout containerNode;//右边名字容器
        private Guideline guideLine;//左右百分比分割线
        private View bottomLine;//圆下面的线
        private TextView tvTime;//左侧的时间
        private TextView tvCircle;//中间的圆
        private TextView tvName;//右侧的名字
        private ZUITextView tvExtras;//右侧名字右边的扩增组件
        private LinearLayout lineContainer;//LineData 容器


        public FlowHolder setTime(String time) {
            if (!StringUtils.isNullOrEmpty(time)) {
                //2023-03-24T14:4143
                String ymd = RegexUtils.getDateYMD(time);
                String hms = RegexUtils.getDateHMS(time);
                if (hms == null) {
                    hms = RegexUtils.getDateHM(time);
                }
                if (!StringUtils.isNullOrEmpty(ymd) && !StringUtils.isNullOrEmpty(hms)) {
                    String s = hms + "\n" + ymd;
                    Spannable formatDate = SpannableUtils.setTextRelativeSize(s, timeSizeProportion, (s.length() - ymd.length()), s.length());
                    SpannableUtils.setTextTypeFace(formatDate, dateTypeface, (s.length() - ymd.length()), s.length());

                    tvTime.setText(formatDate);
                } else if (!StringUtils.isNullOrEmpty(ymd)) {
                    String s = " \n" + ymd;
                    Spannable formatDate = SpannableUtils.setTextRelativeSize(s, timeSizeProportion, (s.length() - ymd.length()), s.length());
                    SpannableUtils.setTextTypeFace(formatDate, dateTypeface, (s.length() - ymd.length()), s.length());

                    tvTime.setText(formatDate);
                } else if (!StringUtils.isNullOrEmpty(hms)) {
                    ymd = " ";
                    String s = hms + "\n" + ymd;
                    Spannable formatDate = SpannableUtils.setTextRelativeSize(s, timeSizeProportion, (s.length() - ymd.length()), s.length());
                    SpannableUtils.setTextTypeFace(formatDate, dateTypeface, (s.length() - ymd.length()), s.length());

                    tvTime.setText(formatDate);
                } else {
                    tvTime.setText(time);
                }
            } else {
                tvTime.setText(null);
            }
            return this;
        }
    }

    /**
     * 点击节点名称监听器
     */
    public interface OnNodeClickListener {
        void onNodeClick(ZUITaskTimeFlowView view, TaskTimeFlowData data);
    }

    /**
     * 点击扩展按钮监听器
     */
    public interface OnExtraButtonClickListener {
        void onExtraButtonClick(ZUITaskTimeFlowView view, TaskTimeFlowData data);
    }

    /**
     * 点击LineData容器监听器
     */
    public interface OnLineDataClickListener {
        void onLineDataClick(ZUITaskTimeFlowView view, TaskTimeFlowData data);
    }

    /**
     * 点击时间监听器
     */
    public interface OnTimeClickListener {
        void onTimeClick(ZUITaskTimeFlowView view, TaskTimeFlowData data);
    }



}
